#include "auditdialog.h"
#include <QSqlRecord>
#include <QDebug> // 添加调试输出头文件

EditUserDialog::EditUserDialog(const QSqlRecord& record, QWidget* parent)
    : QDialog(parent)
{
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    QFormLayout* formLayout = new QFormLayout;
    
    // 从数据库记录中获取用户名和角色信息
    qDebug() << "从数据库获取用户数据:" << record;
    m_usernameEdit = new QLineEdit(record.value("username").toString());
    m_roleEdit = new QLineEdit(record.value("role").toString());
    m_reasonEdit = new QTextEdit;
    qDebug() << "用户数据加载完成";
    
    formLayout->addRow(tr("Username:"), m_usernameEdit);
    formLayout->addRow(tr("Role:"), m_roleEdit);
    formLayout->addRow(tr("Change Reason:"), m_reasonEdit);
    
    QDialogButtonBox* buttonBox = new QDialogButtonBox(
        QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    
    mainLayout->addLayout(formLayout);
    mainLayout->addWidget(buttonBox);
    
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

// 获取修改后的用户数据
QVariantMap EditUserDialog::getNewData() const {
    qDebug() << "获取修改后的用户数据";
    return {
        {"username", m_usernameEdit->text()},
        {"role", m_roleEdit->text()}
    };
}

// 获取变更原因
QString EditUserDialog::getChangeReason() const {
    qDebug() << "获取用户变更原因";
    return m_reasonEdit->toPlainText();
}